package com.xyy.saas.payment.callback.core.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.xyy.elasticjob.client.annotation.ElasticJobConf;
import com.xyy.saas.payment.common.Constants;
import com.xyy.saas.payment.cores.enums.BusinessIdTypeEnum;
import com.xyy.saas.payment.cores.enums.ChannelEnum;
import com.xyy.saas.payment.dao.model.PayResultOrder;
import com.xyy.saas.payment.payment.core.service.ProfitSharingService;
import com.xyy.saas.payment.service.OrderService;
import com.xyy.saas.payment.util.DateUtil;
import com.xyy.saas.payment.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 平安信用支付放款给用户之后定期结算给商户
 */
@Slf4j
@ElasticJobConf(name = "com.xyy.saas.payment.callback.core.job.PinganCreditPayJob", cron = "0 0 9-18 * * ?")
public class PinganCreditPayJob implements SimpleJob {
    @Resource
    private OrderService orderService;
    @Resource
    private ProfitSharingService profitSharingService;

    @Override
    public void execute(ShardingContext shardingContext) {
        long startTime = System.currentTimeMillis();
        log.info("PinganCreditPayJob#execute start.");

        int pageOffset = 0;
        int pageSize = 50;
        int total = 0;
        int totalSuccess = 0;
        int totalFail = 0;
        Date endDate = DateUtil.now();
        Date startDate = DateUtil.getOtherDay(endDate, -Constants.PINGAN_CREDIT_PAY_JOB_OFFSET_DAY);

        while(true) {
            List<PayResultOrder> payResultOrderList = orderService.selectPayResultOrderListByCondition(null, null, null, null,
                    ChannelEnum.PINGAN_CREDIT.getCode(), startDate, endDate, pageOffset, pageSize);
            log.info("PinganCreditPayJob#execute payResultOrderList startDate={}, endDate={}, size={}",
                    DateUtil.format(startDate, DateUtil.DATEFORMATSECOND),
                    DateUtil.format(endDate, DateUtil.DATEFORMATSECOND),
                    payResultOrderList.size());

            if(CollectionUtils.isEmpty(payResultOrderList)) {
                break;
            }

            for (PayResultOrder payResultOrder : payResultOrderList) {
                try {
                    profitSharingService.pinganCreditPaySettle(payResultOrder.getPayNo());
                    totalSuccess++;
                    log.info("PinganCreditPayJob#execute pinganCreditPaySettle success payResultOrder={}", JSONUtils.toJSON(payResultOrder));
                }
                catch(Exception ex) {
                    log.error("PinganCreditPayJob#execute pinganCreditPaySettle error payResultOrder=" + JSONUtils.toJSON(payResultOrder), ex);
                    totalFail++;
                }
            }

            total += payResultOrderList.size();

            if(payResultOrderList.size() < pageSize) {
                break;
            }

            pageOffset += pageSize;
        }

        log.info("PinganCreditPayJob#execute end. total={}, success={}, fail={}, cost={}", total, totalSuccess, totalFail, (System.currentTimeMillis() - startTime));
    }
}
